package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.result.Result;
import com.dongdongshop.result.ResultEnum;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.service.TypeTemplateService;
import com.dongdongshop.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Base64;
import java.util.List;

@Controller
@RequestMapping("typeTemplate")
public class TypeTemplateController {

    private static final Logger logger = LoggerFactory.getLogger(TypeTemplateController.class);

    @Reference
    private BrandService brandService;

    @Reference
    private SpecificationService specificationService;

    @Reference
    private TypeTemplateService typeTemplateService;

    @RequestMapping("toTypeTemplateShow")
    public String toTypeTemplateShow(Model model){
        List<TbBrand> brands = brandService.getBrands();
        List<TbSpecification> specs= specificationService.getSpecs();
        model.addAttribute("brands", brands);
        model.addAttribute("specs", specs);
        return "admin/type_template";
    }

    @RequestMapping(value = "getListTypeTemplate",method = RequestMethod.POST)
    @ResponseBody
    public Result<PageUtil<TbTypeTemplate>> getListTypeTemplate(@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
                                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                String name){
        try {
            PageInfo<TbTypeTemplate> pageInfo = typeTemplateService.getListTypeTemplate(pageNumber,pageSize,name);
            PageUtil<TbTypeTemplate> pageUtil = new PageUtil<>();
            pageUtil.setPageNumber(pageNumber);
            pageUtil.setPageSize(pageSize);
            pageUtil.setList(pageInfo.getList());
            pageUtil.setTotal(pageInfo.getTotal());
            return Result.jsonResponse(ResultEnum.OK).setData(pageUtil);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "insertTypeTemplate",method = RequestMethod.POST)
    @ResponseBody
    public Result insertTypeTemplate(TbTypeTemplate tbTypeTemplate){
        if (null == tbTypeTemplate.getName() || "".equals(tbTypeTemplate.getName()) || null == tbTypeTemplate.getBrandIds()
                || "".equals(tbTypeTemplate.getBrandIds()) || null == tbTypeTemplate.getCustomAttributeItems() || "".equals(tbTypeTemplate.getCustomAttributeItems())
                || null == tbTypeTemplate.getSpecIds() || "".equals(tbTypeTemplate.getSpecIds())){
            return Result.jsonResponse(ResultEnum.BAD_REQUEST);
        }
        try {
            int i = typeTemplateService.insertTypeTemplate(tbTypeTemplate);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "toUpdateTemplate/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Result<TbTypeTemplate> toUpdateTemplate(@PathVariable("id") Long id){
        if (null == id || "".equals(id)){
            return Result.jsonResponse(ResultEnum.BAD_REQUEST);
        }
        try {
            TbTypeTemplate tbTypeTemplate = typeTemplateService.getTemplateById(id);
            return Result.jsonResponse(ResultEnum.OK).setData(tbTypeTemplate);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "updateTemplate",method = RequestMethod.POST)
    @ResponseBody
    public Result updateTemplate(TbTypeTemplate tbTypeTemplate){
        if (null == tbTypeTemplate.getName() || "".equals(tbTypeTemplate.getName()) || null == tbTypeTemplate.getBrandIds()
                || "".equals(tbTypeTemplate.getBrandIds()) || null == tbTypeTemplate.getCustomAttributeItems() || "".equals(tbTypeTemplate.getCustomAttributeItems())
                || null == tbTypeTemplate.getSpecIds() || "".equals(tbTypeTemplate.getSpecIds())){
            return Result.jsonResponse(ResultEnum.BAD_REQUEST);
        }
        try {
            int i = typeTemplateService.updateTemplate(tbTypeTemplate);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "deleteBatch",method = RequestMethod.POST)
    @ResponseBody
    public Result deleteBatch(@RequestParam("ids[]") Long[] ids){
        try {
            int i = typeTemplateService.deleteTemplateBatch(ids);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

}
